public class RequestResponseBodyMethodProcessor extends AbstractMessageConverterMethodProcessor
@RequestBody
and handles return
values from methods annotated with @ResponseBody
by reading and writing
to the body of the request or response with an HttpMessageConverter
.
An @RequestBody
method argument is also validated if it is annotated
with @javax.validation.Valid
. In case of validation failure,
MethodArgumentNotValidException
is raised and results in an HTTP 400
response status code if DefaultHandlerExceptionResolver
is configured.
allSupportedMediaTypes, logger, messageConverters
Constructor and Description |
---|
RequestResponseBodyMethodProcessor(java.util.List<HttpMessageConverter<?>> messageConverters) |
RequestResponseBodyMethodProcessor(java.util.List<HttpMessageConverter<?>> messageConverters,
ContentNegotiationManager contentNegotiationManager) |
Modifier and Type | Method and Description |
---|---|
void |
handleReturnValue(java.lang.Object returnValue,
MethodParameter returnType,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest)
Handle the given return value by adding attributes to the model and
setting a view or setting the
ModelAndViewContainer.setRequestHandled(boolean) flag to true
to indicate the response has been handled directly. |
private boolean |
isBindExceptionRequired(WebDataBinder binder,
MethodParameter methodParam)
Whether to raise a fatal bind exception on validation errors.
|
protected <T> java.lang.Object |
readWithMessageConverters(NativeWebRequest webRequest,
MethodParameter methodParam,
java.lang.reflect.Type paramType)
Create the method argument value of the expected parameter type by
reading from the given request.
|
java.lang.Object |
resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory)
Throws MethodArgumentNotValidException if validation fails.
|
boolean |
supportsParameter(MethodParameter parameter)
Whether the given method parameter is
supported by this resolver.
|
boolean |
supportsReturnType(MethodParameter returnType)
Whether the given method return type is
supported by this handler.
|
private void |
validate(WebDataBinder binder,
MethodParameter methodParam)
Validate the request part if applicable.
|
createOutputMessage, getProducibleMediaTypes, writeWithMessageConverters, writeWithMessageConverters
createInputMessage, readWithMessageConverters
public RequestResponseBodyMethodProcessor(java.util.List<HttpMessageConverter<?>> messageConverters)
public RequestResponseBodyMethodProcessor(java.util.List<HttpMessageConverter<?>> messageConverters, ContentNegotiationManager contentNegotiationManager)
public boolean supportsParameter(MethodParameter parameter)
HandlerMethodArgumentResolver
parameter
- the method parameter to checktrue
if this resolver supports the supplied parameter;
false
otherwisepublic boolean supportsReturnType(MethodParameter returnType)
HandlerMethodReturnValueHandler
returnType
- the method return type to checktrue
if this handler supports the supplied return type;
false
otherwisepublic java.lang.Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws java.lang.Exception
parameter
- the method parameter to resolve. This parameter must
have previously been passed to
HandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter)
and it must have returned true
mavContainer
- the ModelAndViewContainer for the current requestwebRequest
- the current requestbinderFactory
- a factory for creating WebDataBinder
instancesnull
.HttpMessageNotReadableException
- if RequestBody.required()
is true
and there is no body content or if there is no suitable
converter to read the content with.java.lang.Exception
- in case of errors with the preparation of argument valuesprivate void validate(WebDataBinder binder, MethodParameter methodParam) throws MethodArgumentNotValidException
The default implementation checks for @javax.validation.Valid
,
Spring's Validated
,
and custom annotations whose name starts with "Valid".
binder
- the DataBinder to be usedmethodParam
- the method parameterMethodArgumentNotValidException
- in case of a binding error which
is meant to be fatal (i.e. without a declared Errors
parameter)private boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter methodParam)
binder
- the data binder used to perform data bindingmethodParam
- the method argumenttrue
if the next method argument is not of type Errors
protected <T> java.lang.Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter methodParam, java.lang.reflect.Type paramType) throws java.io.IOException, HttpMediaTypeNotSupportedException
AbstractMessageConverterMethodArgumentResolver
readWithMessageConverters
in class AbstractMessageConverterMethodArgumentResolver
T
- the expected type of the argument value to be createdwebRequest
- the current requestmethodParam
- the method argumentparamType
- the type of the argument value to be createdjava.io.IOException
- if the reading from the request failsHttpMediaTypeNotSupportedException
- if no suitable message converter is foundpublic void handleReturnValue(java.lang.Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws java.io.IOException, HttpMediaTypeNotAcceptableException
HandlerMethodReturnValueHandler
ModelAndViewContainer.setRequestHandled(boolean)
flag to true
to indicate the response has been handled directly.returnValue
- the value returned from the handler methodreturnType
- the type of the return value. This type must have
previously been passed to
HandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)
and it must have returned true
mavContainer
- the ModelAndViewContainer for the current requestwebRequest
- the current requestjava.io.IOException
HttpMediaTypeNotAcceptableException